<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-03-15 23:52:29
 * @LastEditTime: 2022-05-20 20:56:20
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:Excel 文件上传
-->
<template>
  <div class="upload-excel">
    <div class="btn-upload">
      <el-button
        :loading="loading"
        type="primary"
        @click="handleUpload"
      >{{ $t('uploadExcel.upload') }}</el-button>
    </div>
    <input
      type="file"
      ref="excelUploadInput"
      class="excel-upload-input"
      accept=".xlsx, .xls"
      @change="handleChange"
    />
    <div
      class="drop"
      @drop.stop.prevent="handleDrop"
      @dragover.stop.prevent="handleDragover"
      @dragenter.stop.prevent="handleDragover"
    >
      <el-icon>
        <upload-filled />
      </el-icon>
      <span>{{ $t('uploadExcel.drop') }}</span>
    </div>
  </div>
</template>

<script setup>
import XLSX from 'xlsx'
import { getHeaderRow, isExcel } from './utils'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
const loading = ref(false)
// eslint-disable-next-line no-undef
const props = defineProps({
  // 上传前回调
  beforeUpload: Function,
  // 上传成功回调
  onSuccess: Function
})
// 点击上传触发
const excelUploadInput = ref(null)
const handleUpload = () => {
  excelUploadInput.value.click()
}
const handleChange = (e) => {
  const files = e.target.files
  const rawFile = files[0]
  if (!rawFile) return
  upload(rawFile)
}
// 触发上传
const upload = (rawFile) => {
  excelUploadInput.value.value = null
  if (!props.beforeUpload) {
    readerData(rawFile)
    return
  }
  const before = props.beforeUpload(rawFile)
  if (before) {
    readerData(rawFile)
  }
}
// 拖拽上传
const handleDrop = (e) => {
  if (loading.value) return
  const files = e.dataTransfer.files
  console.log(files)
  if (files.length !== 1) {
    return ElMessage.error('必须要有一个文件')
  }
  const rawFile = files[0]
  console.log(rawFile)
  if (!isExcel(rawFile)) {
    return ElMessage.error('文件格式不对')
  }
  upload(rawFile)
}
const handleDragover = e => {
  // 在新位置生成源项的副本
  e.dataTransfer.dropEffect = 'copy'
}
// 读取数据 异步
const readerData = rawFile => {
  loading.value = true
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = e => {
      // 1.获取解析到的数据
      const data = e.target.result
      // 2.利用 XLSX 对数据进行解析
      const workbook = XLSX.read(data, {
        type: 'array'
      })
      // 3.获取第一张表格名称
      const firstSheetName = workbook.SheetNames[0]
      // 4.只读取Sheet1的数据
      const worksheet = workbook.Sheets[firstSheetName]
      // 5.解析数据表头
      const header = getHeaderRow(worksheet)
      // 6.解析数据体
      const results = XLSX.utils.sheet_to_json(worksheet)
      // 7.传入解析后的数据
      generateData({ header, results })
      // 8.loading 处理
      loading.value = false
      // 9.异步完成
      resolve()
    }
    reader.readAsArrayBuffer(rawFile)
  })
}
// 根据导入内容,生成数据
const generateData = excelData => {
  props.onSuccess && props.onSuccess(excelData)
}
</script>

<style lang='scss' scoped>
.upload-excel {
  display: flex;
  justify-content: center;
  margin-top: 100px;
  .excel-upload-input {
    display: none;
    z-index: -9999;
  }
  .btn-upload,
  .drop {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px dashed #bbb;
    width: 350px;
    height: 160px;
    text-align: center;
    line-height: 160px;
  }
  .drop {
    line-height: 60px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    color: #bbb;
    i {
      font-size: 60px;
      display: block;
    }
  }
}
</style>
